package br.com.skytef.beans.WEB;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

@ManagedBean
@SessionScoped
public class ManualUsuarioBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private byte[] arquivoPdf;
	private String arquivo;
	private String endereco;
	
	public String getArquivo() {
		return arquivo;
	}
	
	public void setArquivo(String arquivo) {
		this.arquivo = arquivo;
	}
	
	public void importarArquivo() {
		try {
			File file = new File(arquivo);
			arquivoPdf = new byte[(int) file.length()];
			endereco = "/resources/manual/Teste.pdf";
			InputStream is = new FileInputStream(file);
			is.read(arquivoPdf); // faz a leitura e transforma em um array de bytes
			is.close(); // fecha o InputStream e libera seus recursos
						
		} catch (Exception e ) {
			e.printStackTrace();
		}
	}
	
	
	public void visualizaPdf() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		
		HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
		
		response.reset();
		response.setContentType("application/pdf");
		
		response.setContentLength(arquivoPdf.length);
		
		response.setHeader("Content-disposition", "inline;filename=Teste.pdf");
		
		try {
			response.getOutputStream().write(arquivoPdf);
			
			response.getOutputStream().flush();
			
			response.getOutputStream().close();
			
			facesContext.responseComplete();
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
			
		
	}

	public String getEndereco() {
		return endereco;
	}

	public void setEndereco(String endereco) {
		this.endereco = endereco;
	}
	
	
}
